package com.tudouji.project.product.service.impl;

import com.tudouji.common.utils.DateUtils;
import com.tudouji.common.utils.IdUtils;
import com.tudouji.common.utils.SecurityUtils;
import com.tudouji.project.bill.domain.Sellitem;
import com.tudouji.project.product.domain.Salesspecification;
import com.tudouji.project.product.domain.Suitpack;
import com.tudouji.project.product.mapper.SuitpackMapper;
import com.tudouji.project.product.service.ISuitpackService;
import com.tudouji.project.system.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 套装定义Service业务层处理
 *
 * @author tudouji
 * @date 2022-06-27
 */
@Service
public class SuitpackServiceImpl implements ISuitpackService
{
    @Autowired
    private SuitpackMapper suitpackMapper;

    /**
     * 查询套装定义
     *
     * @param guid 套装定义ID
     * @return 套装定义
     */
    @Override
    public Suitpack selectSuitpackById(String guid)
    {
        return suitpackMapper.selectSuitpackById(guid);
    }

    /**
     * 查询套装定义列表
     *
     * @param suitpack 套装定义
     * @return 套装定义
     */
    @Override
    public List<Suitpack> selectSuitpackList(Suitpack suitpack)
    {
        return suitpackMapper.page(suitpack);
    }

    /**
     * 新增套装定义
     *
     * @param suitpack 套装定义
     * @return 结果
     */
    @Override
    public int insertSuitpack(Suitpack suitpack)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        suitpack.setGuid(IdUtils.fastSimpleUUID());
        suitpack.setOperatorid(user.getTuserId());
        suitpack.setOperator(user.getNickName());
        suitpack.setCreatetime(DateUtils.getNowDate());
        return suitpackMapper.insertSuitpack(suitpack);
    }

    /**
     * 修改套装定义
     *
     * @param suitpack 套装定义
     * @return 结果
     */
    @Override
    public int updateSuitpack(Suitpack suitpack)
    {
        return suitpackMapper.updateSuitpack(suitpack);
    }

    /**
     * 批量删除套装定义
     *
     * @param guids 需要删除的套装定义ID
     * @return 结果
     */
    @Override
    public int deleteSuitpackByIds(String[] guids)
    {
        return suitpackMapper.deleteSuitpackByIds(guids);
    }

    /**
     * 删除套装定义信息
     *
     * @param guid 套装定义ID
     * @return 结果
     */
    @Override
    public int deleteSuitpackById(String guid)
    {
        return suitpackMapper.deleteSuitpackById(guid);
    }




    /**
     * @Author shaodong
     * @Description 通过套装ID获取产品集合,并组装成销售明细
     * @Date 18:37 2020/9/16
     * @Param
     * @return
     **/
    public List<Sellitem> findchildSp(Sellitem sellitem, String suitpackid, String suitname){
        ArrayList<Sellitem> items = new ArrayList<>();
        List<Salesspecification> sps = this.suitpackMapper.findchildSp(sellitem.getSpguid());
        if (sps.size() > 0){//
            for (Salesspecification sp : sps){
                Sellitem item = new Sellitem();
                item.setStockunit(sp.getStockunit());
                item.setSfunitid(sp.getSfunitid());
                item.setUnit(sp.getUnit());
                //20240129 备注：出入库单明细添加销售单位id,名称，转换率，销售明细添加转换率
                // 等到跑几个月之后，明细中有了这些数据之后，将出入库时从物料查找销售单位修改为从出入库明细中获取
                // 再跑几个月之后，修改推送任务中获取销售单位的逻辑
                item.setBusiunitTostockunitRate(sp.getNetweight());

                item.setSuitpackid(suitpackid);
                // 把查出来的明细加进去，去除套装明细，写入套装新表
                item.setSpname(sp.getName());
                item.setSpguid(sp.getGuid());
                item.setProductname(sp.getProductname());
                item.setProductguid(sp.getProductguid());
                item.setDeductrate(sellitem.getDeductrate());
                item.setPrice(sp.getSpprice());
                item.setSettletype(sp.getValuation());
                item.setWeight(sellitem.getWeight().multiply(new BigDecimal(sp.getSpamount())));
                item.setHandselcount((sellitem.getHandselcount() == null ? 0:sellitem.getHandselcount())*(sp.getSpamount()));
                item.setSuitname(suitname);
                items.add(item);
            }
        }
        return items;
    }
}
